<?php
//
// Copyright ReccThis.com
// All Rights Reserved
//

class clsReccBusiness extends cDycmsObject
{
    protected $mClsId = 'ReccBusiness';

	protected $mTags;
	protected $mRecommendations;
	
	function Render(cDycmsClientNode &$rClient, $disp = true)
	{
		$rClient->AddChild(new cHtmlHyperlink($this->GetUrl(),
			$this->GetSummary(), array(
				'class' => 'reccBizName')));
		
		$rClient->AddChild($recc = new cUiLayer(array(
			'class' => 'reccBox')));
		
		
		// TODO: real area
		$rClient->AddChild(uiGetIcon('building').'Oxford, UK');
		
		
		//
		// Print the tags
		//
		
		$rClient->AddChild($tagZone = new cUiTagZone($this->mTags));
		
		$recc->AddChild($this->mRecommendations->GetSummary());
		$recc->AddChild(new cUiSeperator());
		
		$recc->AddChild(new cHtmlHyperlink(dyEditorUrl('ReccRecc', 0, array(
			'business' => '5')), uiGetIcon('building_add').'Recc It'));
			
		if($rClient == cUiPage::GetInst()->ContentNode()){
			
			//
			// Now for the recommendations
			//
			
			$rClient->AddChild(new cUiHeading('Recommended By...', 2));
			$this->mRecommendations->Render($rClient);
		};
		
	}

	function DoEdit($vzAttrib){
		
		//
		// Splice tags into the business's tag array
		//
		
		
		if(isset($vzAttrib['tags']) && $vzAttrib['tags'] != ''){
			$this->mTags = array_unique(array_merge(
				array_map(trim,explode(',',$vzAttrib['tags'])),
				$this->mTags));
		};
		
		// TODO: areas
		
		if(isset($vzAttrib['name']))
			return parent::DoEdit(array(
				'name' => $vzAttrib['name'],
				'location' => $vzAttrib['location'],
				'postcode' => $vzAttrib['postcode']
			));
	}
	
	function Write()
	{
		parent::Write();
		
		//
		// Extract tags, and write them to the database
		//
		
		foreach($this->mTags as $tag)
			$this->mClass->SetIndexValue($this->mId, 'tag', $tag);
	}
	
	function DrawEditor(cUiEditor &$editor)
	{
		//
		// Draw the editor form
		//
		
	}
	
	function DrawPublisher(cUiPublisher &$rPublisher)
	{
		throw new dyExInvalidOp("Businesses cannot be published");
	}
	
	function DoPublish()
	{
		throw new dyExInvalidOp("Businesses cannot be published");
	}
	
	function GetSummary()
	{
    	return $this->mAttributes['name'];
	}

	function LoadObject()
	{
        $this->mTags = $this->mClass->GetIndexValue($this->mId, 'tag');
		if(!is_array($this->mTags)) $this->mTags = array($this->mTags);
		$this->mRecommendations = new cDycmsObjectQuery('ReccRecc');
		$this->mRecommendations->AddArray(array('business' => $this->mId));
		$this->mRecommendations->Execute(false, true);
	}

    function Create(){
        $this->mAttributes['name'] = '';
		$this->mAttributes['location'] = '';
		$this->mTags = array();
    }
}

class cCollectionReccBusiness extends cDycmsAbstractCollection
{
	function GetDisplay()
	{
		$buf = "";
		foreach($this->mObject as $obj)
		{
			$buf .= "<div class='article'>\n";
			$buf .= $obj->GetDisplay(NULL, false);
			$buf .= "</div>\n";
		};
		return $buf;
	}

	public function GetSummary($i = -1)
	{
		if($i > -1)
			return $this->mObject[$i]->GetSummary();

		$vz = $this->GetUniqueValues('site');
		$n = count($vz);
		if($n != 1)
			return "Articles";
		$buf = "/ {$vz[0]}";

		$vz = $this->GetUniqueValues('category');
		$n = count($vz);
		if($n != 1)
			return $buf;
		$buf .= " / {$vz[0]}";
		return $buf;
	}
}

?>
